package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmLoginDto dto) {
       //1. 判断参数是否为空
        if(StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2. 判断用户是否存在 select * from wm_user where name=?
        WmUser wmUser = this.lambdaQuery().eq(WmUser::getName, dto.getName()).one();
        if(wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"自媒体用户不存在");
        }

        //3. 判断密码是否正确
        String loginPwd = dto.getPassword();
        String dbPwd = wmUser.getPassword();
        boolean result = BCrypt.checkpw(loginPwd, dbPwd);
        if(!result){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //4. 判断状态是否正确
        if(wmUser.getStatus()!=9){
            return ResponseResult.errorResult(AppHttpCodeEnum.INVALID_STATUS);
        }

        //5. 生成Token
        String token = AppJwtUtil.getToken(wmUser.getId().longValue());
        Map resultMap=new HashMap();
        resultMap.put("token", token);
        wmUser.setPassword("");
        resultMap.put("user", wmUser);

        return ResponseResult.okResult(resultMap);
    }
}